package practise.alogrithm.gotoSiliconValley.array;

import java.util.HashMap;
import java.util.Map;

/**
 * a method contain save(int input) for saving the number, and test(int target) methods for check if there are any two number whose sum equals to the target.
 * Note, there might be some duplicated numbers
 */
public class SumOfTwoNumberIII {
    private Map<Integer, Integer> valueTimesMap = new HashMap<>();
    public void save(int input){
        if(valueTimesMap.containsKey(input)){
            valueTimesMap.put(input,valueTimesMap.get(input)+1);
        }
        else{
            valueTimesMap.put(input,1);
        }
    }
    public boolean test(int target){
        if(target%2==0&&valueTimesMap.get(target/2)>1){
            return true;
        }
        else {
            for(int value:valueTimesMap.keySet()){
                if(valueTimesMap.containsKey(target-value)){
                    return true;
                }
            }
        }
        return false;
    }
}

